package com.ssmk.module.shoppingcar.order;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.liaoinstan.springview.widget.SpringView;
import com.ssmk.R;
import com.ssmk.base.MVPBaseFragment;
import com.ssmk.module.home.view.Home_LoadMoreFooter;
import com.ssmk.module.home.view.Home_RefreshHead;
import com.ssmk.module.shoppingcar.adapter.AllOrderAdapter;
import com.ssmk.module.shoppingcar.adapter.SaleAfterAdapter;
import com.ssmk.module.shoppingcar.bean.OrderAllBean;
import com.ssmk.module.shoppingcar.bean.SaleAfterBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;



public class AllOrder_Fragment_sale extends MVPBaseFragment {
    @BindView(R.id.allorder_springView)
    SpringView mAllorderSpringView;
    @BindView(R.id.allorder_recycler)
    RecyclerView mAllorderRecycler;
    Unbinder unbinder;
    private List<OrderAllBean.DataBean> list = new ArrayList<>();
    private AllOrderAdapter adapter;
    int page = 0;
    LinearLayoutManager manager = new LinearLayoutManager(getActivity());

    @Override
    protected void initdata() {

    }

    @Override
    protected void initView() {
        String mString = getString(R.string.saleAfter);//售后

        //final AllOrderPresenter mAllOrderPresenter = new AllOrderPresenter(getActivity(), this, mString, page);
        //mAllOrderPresenter.GetDataModel();

        mAllorderSpringView.setType(SpringView.Type.FOLLOW);
        mAllorderSpringView.setHeader(new Home_RefreshHead());
        mAllorderSpringView.setFooter(new Home_LoadMoreFooter());
        mAllorderSpringView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                mAllorderSpringView.onFinishFreshAndLoad();
            }
            @Override
            public void onLoadmore() {
                page++;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //mAllOrderPresenter.GetDataModel();
                        mAllorderSpringView.onFinishFreshAndLoad();
                    }
                }, 2000);
            }
        });
    }

    @Override
    protected int layoutRes() {
        return R.layout.allorder_fragment;
    }

    @Override
    public void UpData(Object o) {
        if (o != null) {
            //售后
            if (o instanceof SaleAfterBean) {
                SaleAfterBean saleAfterBean = (SaleAfterBean) o;
                List<SaleAfterBean.DataBean.RowsBean> rows = saleAfterBean.getData().getRows();
                if (rows != null && rows.size() > 0) {
                    mAllorderRecycler.setLayoutManager(manager);
                    SaleAfterAdapter saleAfterAdapter = new SaleAfterAdapter(getActivity(), rows);
                    mAllorderRecycler.setAdapter(saleAfterAdapter);

                    saleAfterAdapter.setOnClickListener(new SaleAfterAdapter.OnClickListener() {
                        @Override
                        public void onClick(View view, int position) {
                            /*(Button)view.*/
                        }
                    });
                }
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
